package com.ruoyi.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.io.IOUtils;
@Slf4j
public class WebServiceUtil {

	public static String PostMessage(String wsdl, String body,String token) {
		String contentType = "application/json";
		int timeout = 2000000;
		HttpClient client = new HttpClient();
		PostMethod postMethod = new PostMethod(wsdl);
		// 设置连接超时
		client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
		// 设置读取时间超时
		client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
		RequestEntity requestEntity = new StringRequestEntity(body);
		if(token!=null){
			postMethod.setRequestHeader(new Header("API_TOKEN",token));
		}
		postMethod.setRequestHeader(new Header("Content-Type",contentType));

		String result = null;
		try {
			// 设置请求体
			postMethod.setRequestEntity(requestEntity);
			int status = client.executeMethod(postMethod);
			// 打印请求状态码
			log.debug("status:" + status);
			// 获取响应体输入流
			InputStream is = postMethod.getResponseBodyAsStream();
			byte[] by = IOUtils.toByteArray(is);
			is.read(by);
			// 获取请求结果字符串
			result = new String(by,"utf-8");
			log.debug("utf-8-------------------------------------" + result);

			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}
		return result;

	}


	public static String GetMessage(String wsdl, String body,String token) {
//		String contentType = "application/soap+xml;charset=UTF-8;action=\"urn:sap-com:document:sap:soap:functions:mc-style:Z_B2B_LONG_TEXT:ZB2bLongTextRequest\"";
		String contentType = "application/json";
		int timeout = 2000000;
		HttpClient client = new HttpClient();
		GetMethod postMethod = new GetMethod(wsdl);
		// 设置连接超时
		client.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
		// 设置读取时间超时
		client.getHttpConnectionManager().getParams().setSoTimeout(timeout);
		//RequestEntity requestEntity = new StringRequestEntity(body);
		if(token!=null){
			postMethod.setRequestHeader(new Header("API_TOKEN",token));
		}
		postMethod.setRequestHeader(new Header("Content-Type",contentType));

		String result = null;
		try {
			int status = client.executeMethod(postMethod);
			// 打印请求状态码
			System.out.println("status:" + status);
			// 获取响应体输入流
			InputStream is = postMethod.getResponseBodyAsStream();
			byte[] by = IOUtils.toByteArray(is);
			is.read(by);
			// 获取请求结果字符串
			result = new String(by,"utf-8");
			System.out.println("utf-8-------------------------------------" + result);

		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
		return result;

	}

	public static void main(String[] args) {
		String json = "{\n" +
			"    \"appkey\": \"RVpqZp6EAG36iDVE5p8abCT3\",\n" +
			"    \"masterSecret\": \"0z4BnyCeGNUJUHrSfM7YN3V4XSc6io\"\n" +
			"}";
		//PostMessage("https://www.zxyun119.com:443/thirdPartyApi/login/api/2.0/auth/getToken",json,null);
		GetMessage("https://www.zxyun119.com:443/thirdPartyApi/ThreePlaceREST/api/2.0/project/getProjectListInfo?pageNum=1&pageSize=10",json,"97e95972-5527-4fb2-864c-2b6bcdcc547a");
	}
}
